Introduction
Plotly is more than an r package and in fact, it is a general data visualization platform.
Today, let’s focus on its r lib– plotly.
To get our hands dirty, we need to download the package in r.
if(!require(plotly)) {
install.packages("plotly")
# or download from github
# devtools::install_github("ropensci/plotly")
}
# load library
library(plotly)
Main Function
In plotly, the main function is plot_ly.
function (data = data.frame(), ..., type = NULL, color, colors = NULL,
alpha = 1, symbol, symbols = NULL, size, sizes = c(10, 100),
linetype, linetypes = NULL, split, frame, width = NULL, height = NULL,
source = "A")
NULL
- data: A data frame (optional) or SharedData object.
- …: These arguments are documented at https://plot.ly/r/reference/ Note that acceptable arguments depend on the value of type.
- type: A character string describing the type of trace.
- color: A formula containing a name or expression. Values are scaled and mapped to color codes based on the value of colors and alpha. To avoid scaling, wrap with I(), and provide value(s) that can be converted to rgb color codes by col2rgb().
- colors: Either a colorbrewer2.org palette name (e.g. “YlOrRd” or “Blues”), or a vector of colors to interpolate in hexadecimal “#RRGGBB” format, or a color interpolation function like colorRamp().
- alpha: A number between 0 and 1 specifying the alpha channel applied to color.
- symbol: A formula containing a name or expression. Values are scaled and mapped to symbols based on the value of symbols. To avoid scaling, wrap with I(), and provide valid pch() values and/or valid plotly symbol(s) as a strin.
- symbols: A character vector of symbol types. Either valid pch or plotly symbol codes may be supplied.
Examples
I will demonstrate several examples here to show what plotly can do for us with r. You can also access this cheatsheet to plot with plotly quickly.
First, let’s generate some sample data.
set.seed(123)
x <- runif(n = 100, min = 1, max = 100)
y <- runif(n = 100, min = 1, max = 100)
y_new <- runif(n = 100, min = 1, max = 100)
library(dplyr)
plot_ly (x = x, y = y, type = 'scatter' ,mode = 'markers',
size = runif(n = 100, min = 1, max = 10),
marker = list(color = sample(c("red","green","orange","pink","blue"),
size = 100,replace = TRUE)))
plot_ly (y = rnorm(n = 100,mean = 0,sd = 1) , type="box") %>%
add_trace(y=rnorm(n=100,mean = 0,sd = 2)) %>%
add_trace(y=rnorm(n=100,mean = 0,sd = 4)) %>%
add_trace(y=rnorm(n=100,mean = 0,sd = 3)) %>%
add_trace(y=rnorm(n=100,mean = 0,sd = 5))
locs <- ggmap::geocode(location = c("Beijing, China", "London, England, United Kingdom",
"New York, NY, United States"))
plot_ly (lon = locs$lon, lat = locs$lat, type = 'scattergeo', mode = "markers",
marker = list(color = c("red", "salmon", "green"), size = c( 30, 40 ,50)))
plot_ly (type = 'choropleth',locations = c('AZ','PA','CA'), locationmode = 'USA-states',
colorscale = 'Viridis', z = c(10, 20 ,30)) %>% layout(geo = list(scopre = 'usa'))
plot_ly (type = 'scattergeo', mode = "markers", lon = locs$lon, lat = locs$lat,
text =c("Beijing, China", "London, England, United Kingdom",
"New York, NY, United States"),
marker = list(color = c("red", "salmon", "green"), size = c( 30, 40 ,50)))
plot_ly (type = 'scatter3d', x = c(9, 8, 5, 1), y = c(1, 2, 4, 8),
z = c(11, 8, 15, 3), mode = "lines")
plot_ly (type = 'scatter3d', x = x, y = y, z = runif(100, 0, 100),
mode = 'markers', size = runif(n = 100, min = 1, max = 10),
marker = list(color = sample(c("red","green","orange"),size = 100,replace = TRUE)))
axis_template <- list(showgrid = F, zeroline = F, nticks = 20,
showline = T, title = "AXIS", mirror = "all")
plot_ly (x =x, y = y, type = "scatter", mode = "markers") %>%
layout(xaxis = axis_template, yaxis = axis_template)
plot_ly (x =x, y = y, type = "scatter", mode = "markers", name = "1") %>%
add_trace(x = x, y = y_new, name = "2") %>%
layout(legend = list( x = 1, y = 1, bgcolor = "white"))